/*
 * Copyright (C) 2011 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.navigation.around;

import android.util.Log;
import ch.bfh.aeneas.exchange.data.Node;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * PoiSorter is small helper class that indexes nodes and their categories and
 * offerst methods to give these categories back.
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class PoiSorter {
    
    private static final String LOG_TAG = "PoiSorter";
    
    private Map<String,ArrayList<Node>> mCategories;
    private ArrayList<Node> mAllNodes;
    
    public PoiSorter() {
        mAllNodes = new ArrayList<Node>();
        mCategories = new HashMap<String,ArrayList<Node>>();
    }
    
    public void addPoi(Node node) {
        mAllNodes.add(node);
        indexPoi(node);
    }
    
    public Set<String> getCategories() {
        return mCategories.keySet();
    }
    
    public String[] getCategoriesArray() {
        Set cats = getCategories();
        String[] cs = new String[cats.size()];
        return (String[]) cats.toArray(cs);
    }
    
    public List<Node> getCategoryNodes(String category) {
        if(mCategories.containsKey(category)) {
            return mCategories.get(category);
        }
        return new ArrayList<Node>();
    }
    
    public int getNodeCount() {
        return mAllNodes.size();
    }
    
    /**
     * Returns all the names of the nodes that are in the specified category.
     * @param category
     * @return 
     */
    public List<String> getNodeNames(String category) {
        List<String> output = new ArrayList<String>();
        List<Node> nodes = getCategoryNodes(category);
        if(nodes != null) {
            for(int i = 0; i < nodes.size(); i++) {
                output.add(nodes.get(i).getName());
            }
        }
        return output;
    }
    
    public String[] getNodeNamesArray(String category) {
        List<String> nodes = getNodeNames(category);
        String[] cs = new String[nodes.size()];
        return (String[]) nodes.toArray(cs);
    }
    
    /**
     * Returns the node at the specified index of the specified category.
     * @param category
     * @param index
     * @return 
     */
    public Node getNodeFromCategory(String category, int index) {
        List<Node> nodes = getCategoryNodes(category);
        Log.i(LOG_TAG, "Trying to get "+nodes.size()+" nodes for category "+category);
        if(index >= 0 && index <= nodes.size()-1) {
            return nodes.get(index);
        }
        return null;
    }

    private void indexPoi(Node node) {
        List<String> nodecat = node.getCategories();
        String category;
        Log.i(LOG_TAG, "Checking node with name "+node.getName()+" for categories...");
        if(nodecat.size() < 1) {
            nodecat = new ArrayList<String>();
            nodecat.add("<none>");
        }
        for(int i = 0; i < nodecat.size(); i++) {
            category = nodecat.get(i);
            Log.i(LOG_TAG, "Found category: "+category);
            if(!mCategories.containsKey(category)) {
                mCategories.put(category,new ArrayList<Node>());
            }
            List<Node> list = mCategories.get(category);
            list.add(node);
        }
    }
}
